package com.haocang.szgm.requestcenter;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.haocang.commonlib.network.interfaces.InitDataCallBack;
import com.haocang.commonlib.network.request.RequestTool;
import com.haocang.szgm.app.MyApplicstion;
import com.haocang.commonlib.network.interfaces.InitDataBeanBack;
import com.haocang.commonlib.network.interfaces.InitDataOneBeanBack;
import com.haocang.szgm.ui.analysis.bean.MpointCurveBean;
import com.haocang.szgm.ui.event.bean.EventBean;
import com.haocang.szgm.ui.event.bean.UserTreeBean;
import com.haocang.szgm.ui.floodpoint.bean.FloodPointBean;
import com.haocang.szgm.ui.home.bean.MsgNumberBean;
import com.haocang.szgm.ui.home.bean.RealTimeTrafficBean;
import com.haocang.szgm.ui.home.bean.SumWaterBean;
import com.haocang.szgm.ui.home.bean.SwicthPumpStationBean;
import com.haocang.szgm.ui.login.bean.DataItemBean;
import com.haocang.szgm.ui.monitor.bean.MapDataBean;
import com.haocang.szgm.ui.monitor.bean.RealDataBean;
import com.haocang.szgm.ui.msg.bean.AlarmBean;
import com.haocang.szgm.ui.msg.bean.MsgBean;
import com.haocang.szgm.ui.pipedata.bean.BillStatisticsBean;
import com.haocang.szgm.ui.pipedata.bean.NetWorkInfoBean;
import com.haocang.szgm.ui.reason.bean.EmergencySolutionBean;
import com.haocang.szgm.ui.report.bean.CompositeIndexListBean;
import com.haocang.szgm.ui.report.bean.PurificationPlantDayBean;
import com.haocang.szgm.ui.user.bean.ArrangementBaseBean;
import com.haocang.szgm.ui.user.bean.GroupUserBean;
import com.haocang.szgm.ui.waterpoint.bean.WaterFlowBean;
import com.haocang.szgm.ui.waterpoint.bean.WaterPointBean;
import com.haocang.szgm.utils.bean.ArrangementBaseInfoBean;
import com.haocang.szgm.utils.bean.ArrangementBean;
import com.haocang.szgm.utils.bean.DeptBean;
import com.haocang.szgm.utils.bean.EarlyWarningActionBean;
import com.haocang.szgm.utils.bean.FileBean;
import com.haocang.szgm.utils.bean.FloodCarBean;
import com.haocang.szgm.utils.bean.ProvinceBean;
import com.haocang.szgm.utils.bean.ReasonBean;
import com.haocang.szgm.utils.bean.ShareIdNameBean;
import com.haocang.szgm.utils.bean.UserBean;
import com.haocang.szgm.utils.bean.XyBean;
import com.haocang.szgm.utils.config.Config;

/**
 * 创建时间：2019/3/4
 * 编 写 人：ShenC
 * 功能描述：
 */

public class DataModel {


    private static final int initialTimeoutMs = 20000;

    /**
     * 封装WebApi 的 GET 请求
     *
     * @param ApiUrl   ：请求地址
     * @param callback
     */
    public static void GetApiRequestMode(final String ModelType, String ApiUrl, final InitDataBeanBack callback) {
        StringRequest jsonObjectRequeststauts = new StringRequest(Request.Method.GET, ApiUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonobject = new JSONObject(response);
                            if (jsonobject.getInt("Code") == 0) {
                                Gson gson = new Gson();

                                //======实体类定义==================================================================
                                if (ModelType.equals("FloodPointBean")) {
                                    List<FloodPointBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<FloodPointBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("WaterPointBean")) {
                                    List<WaterPointBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<WaterPointBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("DeptBean")) {
                                    List<DeptBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<DeptBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("UserBean")) {
                                    List<UserBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<UserBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("ReasonBean")) {
                                    List<ReasonBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<ReasonBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("EventBean")) {
                                    List<EventBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<EventBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("FileBean")) {
                                    List<FileBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<FileBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("MsgBean")) {
                                    List<MsgBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<MsgBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("EarlyWarningActionBean")) {
                                    List<EarlyWarningActionBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<EarlyWarningActionBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("PurificationPlantDayBean")) {
                                    List<PurificationPlantDayBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<PurificationPlantDayBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("CompositeIndexListBean")) {
                                    List<CompositeIndexListBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<CompositeIndexListBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("ArrangementBean")) {
                                    List<ArrangementBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<ArrangementBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("ArrangementBaseInfoBean")) {
                                    List<ArrangementBaseInfoBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<ArrangementBaseInfoBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("ArrangementBaseBean")) {
                                    List<ArrangementBaseBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<ArrangementBaseBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("ShareIdNameBean")) {
                                    List<ShareIdNameBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<ShareIdNameBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");

                                } else if (ModelType.equals("GroupUserBean")) {
                                    List<GroupUserBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<GroupUserBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("WaterFlowBean")) {
                                    List<WaterFlowBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<WaterFlowBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("FloodCarBean")) {
                                    List<FloodCarBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<FloodCarBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("MapDataBean")) {
                                    List<MapDataBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<MapDataBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("RealDataBean")) {
                                    List<RealDataBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<RealDataBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("MpointCurveBean")) {
                                    List<MpointCurveBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<MpointCurveBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("AlarmBean")) {
                                    List<AlarmBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<AlarmBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("UserTreeBean")) {
                                    List<UserTreeBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<UserTreeBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("DataItemBean")) {
                                    List<DataItemBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<DataItemBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("BillStatisticsBean")) {
                                    List<BillStatisticsBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<BillStatisticsBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else if (ModelType.equals("SumWaterBean")) {
                                    List<SumWaterBean> datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<List<SumWaterBean>>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else {
                                    callback.callbak(false, null, "实体未定义");
                                }
                                //====================================================================================


                            } else {
                                callback.callbak(false, null, "错误码：" + jsonobject.getInt("Code") + "," + jsonobject.getString("Message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            callback.callbak(false, null, "解析错误" + e.toString());
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.callbak(false, null, "网络或者服务端错误：" + ModelType);
            }
        });
        jsonObjectRequeststauts.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(MyApplicstion.getAppContext()).addToRequestQueue(jsonObjectRequeststauts);
    }

    /**
     * ===============================================
     * 创建时间：2018/12/20 10:31
     * 编 写 人：ShenC
     * 方法说明：
     * ================================================
     * 修改内容：      修改时间：      修改人：
     * ===============================================
     */
    public static void GetApiRequestOneMode(final String ModelType, String ApiUrl, final InitDataOneBeanBack callback) {
        StringRequest jsonObjectRequeststauts = new StringRequest(Request.Method.GET, ApiUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonobject = new JSONObject(response);
                            if (jsonobject.getInt("Code") == 0) {
                                Gson gson = new Gson();

                                //======s实体类定义==================================================================
                                if (ModelType.equals("ProvinceBean")) {
                                    ProvinceBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<ProvinceBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("WaterPointBean")) {
                                    WaterPointBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<WaterPointBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("MsgNumberBean")) {
                                    MsgNumberBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<MsgNumberBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("PurificationPlantDayBean")) {
                                    PurificationPlantDayBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<PurificationPlantDayBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("XyBean")) {
                                    XyBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<XyBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("EmergencySolutionBean")) {
                                    EmergencySolutionBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<EmergencySolutionBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("NetWorkInfoBean")) {
                                    NetWorkInfoBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<NetWorkInfoBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("RealTimeTrafficBean")) {
                                    RealTimeTrafficBean data = gson.fromJson(jsonobject.getString("Data"), new TypeToken<RealTimeTrafficBean>() {
                                    }.getType());
                                    callback.callbak(true, data, "请求成功");
                                } else if (ModelType.equals("SwicthPumpStationBean")) {
                                    SwicthPumpStationBean datalist = gson.fromJson(jsonobject.getString("Data"), new TypeToken<SwicthPumpStationBean>() {
                                    }.getType());
                                    callback.callbak(true, datalist, "请求成功");
                                } else {
                                    callback.callbak(false, null, "实体未定义");
                                }

                                //====================================================================================

                            } else {
                                callback.callbak(false, null, jsonobject.getString("Message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            callback.callbak(false, null, "解析错误" + e.toString());
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.callbak(false, null, "网络或者服务端错误：" + ModelType);
            }
        });
        jsonObjectRequeststauts.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(MyApplicstion.getAppContext()).addToRequestQueue(jsonObjectRequeststauts);
    }


    /**
     * 封装WebApi 的 GET 请求
     *
     * @param ApiUrl   ：请求地址
     * @param callback
     */
    public static void GetApiRequestObject(final String ModelType, String ApiUrl, final InitDataBeanBack callback) {
        StringRequest jsonObjectRequeststauts = new StringRequest(Request.Method.GET, ApiUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();

                        //======s实体类定义==================================================================
                        if (ModelType.equals("ProvinceBean")) {
                            List<ProvinceBean> datalist = gson.fromJson(response, new TypeToken<List<ProvinceBean>>() {
                            }.getType());
                            callback.callbak(true, datalist, "请求成功");
                        }
                        //====================================================================================
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.callbak(false, null, "网络或者服务端错误");
            }
        });
        jsonObjectRequeststauts.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(MyApplicstion.getAppContext()).addToRequestQueue(jsonObjectRequeststauts);
    }

    /**
     * 封装WebApi 的 GET 请求
     *
     * @param context  ：上下文
     * @param ApiUrl   ：请求地址
     * @param callback
     */
    public static void GetApiRequest(Context context, final String ApiUrl, final InitDataCallBack callback) {
        StringRequest jsonObjectRequeststauts = new StringRequest(Request.Method.GET, ApiUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        callback.callbak(true, response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(Config.LogFlag, "DataModel > GetApiRequest  数据获取失败,url" + ApiUrl);
                callback.callbak(false, "网络错误获取数据失败。");
            }
        });
        jsonObjectRequeststauts.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(context).addToRequestQueue(jsonObjectRequeststauts);
    }

    /**
     * 封装WebApi 的 POST 请求
     *
     * @param context  ：上下文
     * @param ApiUrl   ：请求地址
     * @param callback
     */
    public static void PostApiRequest(Context context, String ApiUrl, HashMap<String, Object> maps, final InitDataCallBack callback) {
        JSONObject params = new JSONObject(maps);
        JsonObjectRequest jsonObjectRequest2 = new JsonObjectRequest(Request.Method.POST, ApiUrl, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i(Config.LogFlag, "DataModel > PostApiRequest  数据获取成功");

                        try {
                            if (response.getInt("Code") == 0) {
                                callback.callbak(true, response.getString("Data"));
                            } else {
                                callback.callbak(false, "" + response.getString("Message"));
                            }
                        } catch (JSONException e) {
                            callback.callbak(false, "解析错误");
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(Config.LogFlag, "DataModel > PostApiRequest  数据获取失败");
                callback.callbak(false, "网络错误获取数据失败。");
            }
        });
        jsonObjectRequest2.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(context).addToRequestQueue(jsonObjectRequest2);
    }

    /**
     * 封装WebApi 的 PUT请求
     *
     * @param context  ：上下文
     * @param ApiUrl   ：请求地址
     * @param callback
     */
    public static void PutApiRequest(Context context, String ApiUrl, HashMap<String, Object> maps, final InitDataCallBack callback) {

        JSONObject params = new JSONObject(maps);
        JsonObjectRequest jsonObjectRequest2 = new JsonObjectRequest(Request.Method.PUT, ApiUrl, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i(Config.LogFlag, "DataModel > PostApiRequest  数据获取成功");

                        try {
                            if (response.getBoolean("Result")) {
                                callback.callbak(true, response.getString("Data"));
                            } else {
                                callback.callbak(false, "数据错误" + response.getString("Msg"));
                            }
                        } catch (JSONException e) {
                            callback.callbak(false, "解析错误");
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(Config.LogFlag, "DataModel > PostApiRequest  数据获取失败");
                callback.callbak(false, "网络错误获取数据失败。");
            }
        });
        //mQueue.add(jsonObjectRequest2);
        jsonObjectRequest2.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(context).addToRequestQueue(jsonObjectRequest2);
    }


    /**
     * 封装WebApi 的 DELETE请求
     *
     * @param context  ：上下文
     * @param ApiUrl   ：请求地址
     * @param callback
     */
    public static void DeleteApiRequest(Context context, String ApiUrl, HashMap<String, Object> maps, final InitDataCallBack callback) {

        RequestQueue mQueue = Volley.newRequestQueue(context);
        JSONObject params = new JSONObject(maps);
        JsonObjectRequest jsonObjectRequest2 = new JsonObjectRequest(Request.Method.DELETE, ApiUrl, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i(Config.LogFlag, "DataModel > PostApiRequest  数据获取成功");

                        try {
                            if (response.getBoolean("Result")) {
                                callback.callbak(true, response.getString("Data"));
                            } else {
                                callback.callbak(false, "数据错误" + response.getString("Msg"));
                            }
                        } catch (JSONException e) {
                            callback.callbak(false, "解析错误");
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(Config.LogFlag, "DataModel > PostApiRequest  数据获取失败");
                callback.callbak(false, "网络错误获取数据失败。");
            }
        });
        jsonObjectRequest2.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, 1, 1.0f));
        RequestTool.getInstance(context).addToRequestQueue(jsonObjectRequest2);
    }


}
